package com.lhl.jwt.filters;

import cn.hutool.core.util.StrUtil;
import com.lhl.jwt.utils.JwtUtil;
import com.lhl.jwt.utils.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@Slf4j
@WebFilter(urlPatterns = "/*")
public class RequestFilter implements Filter {

    public static final String AUTHORIZATION_HEADER = "Authorization";
    public static final String AUTHORIZATION_START_STRING = "Bearer ";
    private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>(
            Arrays.asList(
                    "/swagger-resources",
                    "/swagger",
                    "/static",
                    "/csrf",
                    "/webjars",
                    "/null/swagger-resources",
                    "/swagger-ui.html",
                    "/user/app/login",
                    "/user/login",
                    "/wx/cp/userInfo/1000004",
                    "retail/onLogin",
                    "/user/findPassword",
                    "/user/resetByTelPhone",
                    "/user/getToken",
                    "/error")));
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
        log.debug("path=={}",path);
        boolean allowedPath = ALLOWED_PATHS.contains(path);
        if (allowedPath || canIgnore(request)) {
            log.debug("这里是不需要处理的url进入的方法");
            chain.doFilter(req, res);
        } else {
            log.debug("这里是需要处理的url进入的方法");
            log.debug("请求URL : {}",request.getRequestURL().toString());
            log.debug("HTTP_METHOD : {}",request.getMethod());
            log.debug("IP : {}",request.getRemoteAddr());
            String token = request.getHeader(AUTHORIZATION_HEADER);
            if(StrUtil.isNotBlank(token)) {
                log.debug("token=={}", token);
                String re_token = StrUtil.removePrefix(token, AUTHORIZATION_START_STRING);
                log.debug("re_token=={}", re_token);
                if (JwtUtil.checkToken(re_token)) {
                    String kt = JwtUtil.getUserId(re_token);
                    log.debug("kt=={}",kt);
                    UserInfoUtil.setUserId(kt);
                }
            }
            chain.doFilter(req, res);
        }
    }
    private boolean canIgnore(HttpServletRequest request) {
        String url = request.getRequestURI();
        for (String ignore : ALLOWED_PATHS) {
            if (url.startsWith(ignore)) {
                return true;
            }
        }
        return false;
    }
}
